package cn.skyquant.quant4j.jforex.sdk.strategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 反向加仓管理
 */
public class ReflectManager {
    private final List<ReflectDTO> reflectDTOList = new ArrayList<>();
    public boolean valid;//代表是否已经开启反向加仓

    public ReflectManager(String str){
        if(str!=null){
            String[] ss = str.split(",");
            if(ss!=null && ss.length>0){
                for (String s:ss){
                    String[] rs= s.split(":");
                    if(rs!=null && rs.length==2){
                        try {
                            reflectDTOList.add(new ReflectDTO(Integer.parseInt(rs[0]), Double.parseDouble(rs[1])));
                        }catch (NumberFormatException e){

                        }
                    }
                }
            }
        }
        if(reflectDTOList.size()>0){
            valid = true;
        }else {
            valid = false;
        }
        if(reflectDTOList.size()>1){
            reflectDTOList.sort((a, b) -> b.index - a.index);
        }
    }

    public double get(int index) {
        double result = 1;
        for (ReflectDTO reflect : reflectDTOList) {
            if (index >= reflect.index ){
                return reflect.m;
            }
        }
        return result;
    }


    public static final class ReflectDTO {
        public int index;
        public double m;

        private ReflectDTO(int index, double m) {
            this.index = index;
            this.m = m;
        }
    }

}
